using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    [SerializeField] private Transform target;

    [SerializeField] private float smoothpeed = 0.125f;

    [SerializeField] private Vector3 offset = new Vector3(0, -1, 0);
    
    
    
    [Header("边界限制")]
    [SerializeField] private bool useBounds = true; 
    [SerializeField] private float minX = -10f; 
    [SerializeField] private float maxX = 10f;
    [SerializeField] private float minY = -5f; // Y轴最小值
    [SerializeField] private float maxY = 5f; 
    // Start is called before the first frame update
    void Start()
    {
        if (target == null)
        {
            var player = FindObjectOfType<Player>();
            if (player != null)
            {
                target = player.transform;
            }
        }
    }

    // Update is called once per frame
    void LateUpdate()
    {
        if (target == null) return;
        
        Vector3 desiredPosition = target.position + offset;
        if (useBounds)
        {
            desiredPosition.x = Mathf.Clamp(desiredPosition.x, minX, maxX);
            desiredPosition.y = Mathf.Clamp(desiredPosition.y, minY, maxY);
        }
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothpeed);
        transform.position = new Vector3(smoothedPosition.x,smoothedPosition.y,smoothedPosition.z);
    }
    private void OnDrawGizmosSelected()
    {
        if (useBounds)
        {
            Gizmos.color = Color.yellow;
            Gizmos.DrawLine(new Vector3(minX, minY, 0), new Vector3(minX, maxY, 0));
            Gizmos.DrawLine(new Vector3(maxX, minY, 0), new Vector3(maxX, maxY, 0));
            Gizmos.DrawLine(new Vector3(minX, minY, 0), new Vector3(maxX, minY, 0));
            Gizmos.DrawLine(new Vector3(minX, maxY, 0), new Vector3(maxX, maxY, 0));
        }
    }
}
